package org.jeecg.modules.clues.jobs;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jeecg.modules.clues.entity.Clues;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        WebClient webClient = getClient();
        String result = getHtml("https://space.bilibili.com/36964670?spm_id_from=333.337.0.0", webClient);
        Document doc = Jsoup.parse(result);
        Elements elementsByClass = doc.getElementsByClass("n-data n-gz");
        for (Element byClass : elementsByClass) {

        }
    }


    public  static WebClient  getClient(){
        WebClient  webClient = new WebClient(BrowserVersion.CHROME);

        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setTimeout(30000);
        webClient.setJavaScriptTimeout(30000);
        webClient.getOptions().setRedirectEnabled(true);
        return  webClient;

    }


    public  static String  getHtml(String url, WebClient webClient) throws InterruptedException {
        HtmlPage page = null;
        try {
            webClient.getOptions().setUseInsecureSSL(true);
            page = webClient.getPage(url);
        } catch (Exception e) {
            e.printStackTrace();
            webClient.close();

        }

        //该方法阻塞线程
        webClient.waitForBackgroundJavaScript(30000);
        return   page.asXml();
    }

}
